package org.aeon.gamechatnest.controller;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.aeon.gamechatnest.common.response.Result;
import org.aeon.gamechatnest.common.response.ResultBuild;
import org.aeon.gamechatnest.pojo.UserServer;
import org.aeon.gamechatnest.service.UserServerService;
import org.aeon.gamechatnest.util.SecurityUtil;
import org.aeon.gamechatnest.validated.Add;
import org.aeon.gamechatnest.validated.Update;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author : Liys 2024年5月9日
 */
@Validated
@RestController
@RequestMapping("/user/server")
@Tag(name = "UserServerControllerApi", description = "用户服务器控制器")
public class UserServerController {
    @Autowired
    private UserServerService userServerService;

    @Operation(summary = "进入服务器", parameters = {@Parameter(name = "serverId", description = "服务器编号")})
    @GetMapping("/use/{serverId}")
    public Result useServer(@PathVariable("serverId") Long serverId) {
        boolean result = userServerService.useServer(serverId);
        return ResultBuild.successIf(result).build();
    }

    @Operation(summary = "新增用户服务器")
    @PostMapping
    public Result save(@RequestBody @Validated(Add.class) UserServer userServer) {
        boolean result = userServerService.save(userServer);
        return ResultBuild.successIf(result).build();
    }

    @PreAuthorize("hasAnyAuthority('user-server:del','creator') AND " +
            "#serverId == T(org.aeon.gamechatnest.util.SecurityUtil).server")
    @Operation(summary = "删除用户服务器",
            parameters = {@Parameter(name = "serverId", description = "编号")})
    @DeleteMapping("/{serverId}")
    public Result removeById(@PathVariable("serverId") Long serverId) {
        boolean result = userServerService.removeById(serverId);
        return ResultBuild.successIf(result).build();
    }

    @PreAuthorize("hasAnyAuthority('user-server:mod','admin','creator') AND " +
            "#userServer.serverId == T(org.aeon.gamechatnest.util.SecurityUtil).server")
    @Operation(summary = "修改用户服务器")
    @PutMapping
    public Result modifyById(@RequestBody @Validated(Update.class) UserServer userServer) {
        boolean result = userServerService.modifyById(userServer);
        return ResultBuild.successIf(result).build();
    }

    @Operation(
            summary = "查询用户服务器",
            parameters = {@Parameter(name = "name", description = "服务器名称")}
    )
    @GetMapping("/search")
    public Result findByName(String name) {
        List<UserServer> result = userServerService.findByName(name);
        return ResultBuild.result().successful().data(result).build();
    }

    @Operation(
            summary = "查询当前用户所属服务器"
    )
    @GetMapping("/user")
    public Result findByLoginUser() {
        Long loginUserId = SecurityUtil.getUserId();
        List<UserServer> result = userServerService.findByUserId(loginUserId);
        return ResultBuild.result().successful().data(result).build();
    }

}
